/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;
import junit.framework.TestCase;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;

/*References: 

	http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/RandomStringUtils.html

*/	
		
/**
 * Performs Validation Test for url validations.
 *
 * @version $Revision: 1128446 $ $Date: 2011-05-27 13:29:27 -0700 (Fri, 27 May 2011) $
 */
public class UrlValidatorTest extends TestCase {

   private boolean printStatus = false;
   private boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using.

   @Before 
   public void setup(){
	   
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
   }
   
   
   public UrlValidatorTest(String testName) {
      super(testName);
   }

   
   @Test
   public void testManualTest()
   {
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   
	   String url="http://10.0.0.1:80";
	   if(urlVal.isValid(url)){
		   
		   System.out.println("Manual Test 1: PASS with " + url + " as valid argument.");
		   
	   }
	   else{
		   
		   System.out.println("Manual Test 1: FAIL with " + url + " as valid argument.");
	   }
	   
	   url="http:///10.0.0.1:80";
	   
	   if(!urlVal.isValid(url)){
		   
		   System.out.println("Manual Test 2: PASS with " + url + " as invalid argument.");
		   
	   }
	   else{
		   
		   System.out.println("Manual Test 2: FAIL with " + url + " as invalid argument.");
	   }
	  		   
   }
   
   
   public void testYourFirstPartition()  //this partition will test only http urls
   {
	   String[] schemes = {"http"};
	   UrlValidator urlVal = new UrlValidator(schemes);
	   
	   String url="http://10.0.0.1:80";
	   if(urlVal.isValid(url)){
		   
		   System.out.println("Partition 1 (HTTP) Test 1: PASS with " + url + " as valid argument.");
		   
	   }
	   else{
		   
		   System.out.println("Partition 1 (HTTP) Test 1: FAIL with " + url + " as valid argument.");
	   }
	   
   }
   
   public void testYourSecondPartition() //this partition will test only ftp urls
	   {
		   String[] schemes = {"ftp"};
		   UrlValidator urlVal = new UrlValidator(schemes);
		   
		   String url="ftp://10.0.0.1:80";
		   if(urlVal.isValid(url)){
			   
			   System.out.println("Partition 2 (FTP) Test 1: PASS with " + url + " as valid argument.");
			   
		   }
		   else{
			   
			   System.out.println("Partition 2 (FTP) Test 1: FAIL with " + url + " as valid argument.");
		   }
	   
   }
   
   
   public void testIsValid()
   {
	   System.out.print("Starting testIsValid()...\n");
	     
	   File inFile=new File("urls.txt");
	   Scanner in;
		try {
			in = new Scanner(inFile);
		
			   int linenumber=1;
			   
			   UrlValidator urlVal =new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
			  			   
			   while(in.hasNextLine()){
				   
				   String url=in.nextLine();
				   
				   if(!urlVal.isValid(url)){								//if it's valid was it supposed to be?
		
					   System.out.print("FAIL: testIsValid() reported the url \"" + url + "\" was invalid. \n");
					   
				   }
				}
			   in.close();
	   
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   			   
   }
   
   public void testAnyOtherUnitTest()
   {
	   //String[] schemes = {"http","https"};
	   
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
       String valid_test_query="test query";
              
	   if(urlVal.isValidQuery(valid_test_query)==true){
		   
		   System.out.println("IsValidQuery() Test 1: PASS with " + valid_test_query + " as a valid argument.");
		   
	   }
	   else{
		   
		   System.out.println("IsValidQuery() Test 1: FAIL with " + valid_test_query + " as a valid argument.");
	   }
	   
	   String invalid_test_query="\n";
	   	   
	   if(urlVal.isValidQuery(invalid_test_query)==false){
		   
		   System.out.println("IsValidQuery() Test 2: PASS with a newline as an invalid argument.");
		   
	   }
	   else{
		   
		   System.out.println("IsValidQuery() Test 2: FAIL with a newline as an invalid argument.");
	   }
	   
	   String blank_test_query=null;
	   
	   if(urlVal.isValidQuery(blank_test_query)==true){
		   
		   System.out.println("IsValidQuery() Test 3: PASS with a null as an argument.");
		   
	   }
	   else{
		   
		   System.out.println("IsValidQuery() Test 3: FAIL with a null as an argument.");
	   }
	   
   }
   /**
    * Create set of tests by taking the testUrlXXX arrays and
    * running through all possible permutations of their combinations.
    *
    * @param testObjects Used to create a url.
    */
   
   //private static final String QUERY_REGEX = "^(.*)$";
   
   //private static final Pattern QUERY_PATTERN = Pattern.compile(QUERY_REGEX);
   
}
